//
//  MainContentView.m
//  nuoee_krypto
//
//  Created by Mac on 2018/5/31.
//  Copyright © 2018年 nuoee. All rights reserved.
//

#import "MainContentView.h"
#import "MainContentCell.h"

@interface MainContentView()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, assign) CoinRankOrExchangeType type;


@end

@implementation MainContentView

- (instancetype)initWithType:(CoinRankOrExchangeType)type{
    if (self == [super init]) {
        
        self.type = type;
        self.delegate = self;
        self.dataSource = self;
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
        self.backgroundColor = MainBlackColor;
    }
    return self;
}

- (void)setCurrentOffset:(CGPoint)currentOffset{
    _currentOffset = currentOffset;
    @synchronized(self)
    {
        for (MainContentCell* c in self.visibleCells) {
            c.collectionView.contentOffset = _currentOffset;
        }
    }
}

#pragma mark - tableView datasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 20;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MainContentCell *cell = [MainContentCell mainContentCell:tableView type:self.type];
    cell.index = indexPath.row;
    WeakSelf(self);
    cell.offsetBlock = ^(CGPoint offset){
        @synchronized(weakself)
        {
            weakself.currentOffset = offset;
            if ([weakself.mainDelegate respondsToSelector:@selector(mainContentCurrentOffset:)]) {
                [weakself.mainDelegate mainContentCurrentOffset:offset];
            }
            for (MainContentCell* c in weakself.visibleCells) {
                c.collectionView.contentOffset = weakself.currentOffset;
            }
        }
    };
    return cell;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //处理cell复用
    MainContentCell *c = (MainContentCell *)cell;
    c.collectionView.contentOffset = self.currentOffset;
}

#pragma mark - tableView delegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return AdaptY(47);
}

@end
